﻿using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DLV.BackEnd.Pages
{
    public partial class EditPriceItem : AdminPageBase
    {
        protected int Type = 0;

        private PriceItemInfo _priceItemInfo = new PriceItemInfo();
        private readonly PriceItemController _priceItemController = new PriceItemController();

       


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["Type"]))
            {
                Type = int.Parse(Request.QueryString["Type"]);
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PriceItemId"]))
                _priceItemInfo = _priceItemController.GetPriceItem(int.Parse(Request.QueryString["PriceItemId"]));
            
            if (Page.IsPostBack) return;
                if (Type == 4 || Type == 7 || Type == 8)
                {
                    LoadPriceByType();
                }
                LoadSchedule();
                LoadUser();
                LoadStatus();
            if (_priceItemInfo.PriceItemId == -1) return;
                if (Type == 1 || Type == 2 || Type == 3)
                { 
                    LoadPriceBySchedule(); 
                }
                
                
                RenderForm();
            
        }
        
        protected void lbtSave_Click(object sender, EventArgs e)
        {
             SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            RedirectLink();
            
        }

        private void SaveData()
        {
            var priceInfo=new PriceController().GetPrice(Convert.ToInt32(Request["ctl00$placeHolderContent$ddlPrice"]));
            
            var ageInfo= new AgeController().GetAge(priceInfo.AgeId);
            var nationalInfo = new NationalController().GetNational(priceInfo.NationalId);
            

            _priceItemInfo.ScheduleId = int.Parse(ddlSchedule.SelectedValue);
            _priceItemInfo.PriceId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlPrice"]);
            _priceItemInfo.Quantity = int.Parse(txtQuantity.Text);
            _priceItemInfo.PriceItemType = Type;
            _priceItemInfo.Status = int.Parse(ddlStatus.SelectedValue);
            _priceItemInfo.TotalPrice = priceInfo.SalePrice * decimal.Parse(txtQuantity.Text);
            _priceItemInfo.GoDate = DateTime.Parse(dteGoDate.Value);
            if (Type == 1 || Type == 2 || Type == 3)
            {
                var ticketInfo = new TicketController().GetTicket(priceInfo.ItemId);
                var concernInfo = new ConcernController().GetConcern(ticketInfo.ConcernId);
                var typeInfo = new TypeController().GetType(ticketInfo.TypeId);
                _priceItemInfo.PriceItemName = concernInfo.ConcernSortName + " - " + typeInfo.TypeName + " - " + ticketInfo.TicketName + " - " + ageInfo.AgeName + " - " + priceInfo.Time+" - "+nationalInfo.NationalName;
            }
            else if (Type == 4)
            {
                var menu = new MenuController().GetMenu(priceInfo.ItemId);
                var restaurant = new RestaurantController().GetRestaurant(menu.RestaurantId);
                var menuType = new MenuTypeController().GetMenuType(menu.MenuTypeId);
                _priceItemInfo.PriceItemName = restaurant.RestaurantSortName + " - " + menuType.MenuTypeName + " - " + ageInfo.AgeName + " - " + nationalInfo.NationalName;

            }
            else if(Type==7)
            {
                var insurance = new InsuranceController().GetInsurance(priceInfo.ItemId);
                var partner = new PartnerController().GetPartner(insurance.PartnerId);
                _priceItemInfo.PriceItemName = partner.PartnerSortName + " - " + insurance.InsuranceName + " - " + ageInfo.AgeName + " - " + nationalInfo.NationalName;

            }
            else if (Type == 8)
            {
                var service = new ServiceController().GetService(priceInfo.ItemId);
                var location = new LocationController().GetLocation(service.LocationId);
                _priceItemInfo.PriceItemName = location.LocationName + " - " + service.ServiceName + " - " + ageInfo.AgeName + " - " + nationalInfo.NationalName;

            }

            if (_priceItemInfo.PriceItemId == -1)
            {
                _priceItemInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _priceItemInfo.CreatedDate = DateTime.Now;
                _priceItemController.InsertPriceItem(_priceItemInfo);
            }
            else
            {
                _priceItemInfo.UpdatedBy = int.Parse(ddlCreatedBy.SelectedValue);
                _priceItemInfo.UpdatedDate = DateTime.Now;
                _priceItemController.UpdatePriceItem(_priceItemInfo);
            }

            RedirectLink();
        }
        private void RenderForm()
        {            
            ddlPrice.SelectedValue = _priceItemInfo.PriceId.ToString();           
            txtQuantity.Text = _priceItemInfo.Quantity.ToString();
            ddlSchedule.SelectedValue = _priceItemInfo.ScheduleId.ToString();
            ddlCreatedBy.SelectedValue = _priceItemInfo.CreatedBy.ToString();
            ddlStatus.SelectedValue = _priceItemInfo.Status.ToString();
            dteGoDate.Value = _priceItemInfo.GoDate.ToString("dd/MM/yyyy");
        }
        private void RedirectLink()
        {
            var link = String.Empty;
            switch (Type)
            {
                case 1:
                    link = "/Pages/ListPriceItem.aspx?xml=PriceItemAir&Type=" + Type;
                    break;
                case 2:
                    link = "/Pages/ListPriceItem.aspx?xml=PriceItemStation&Type=" + Type;
                    break;
                case 3:
                    link = "/Pages/ListPriceItem.aspx?xml=PriceItemCar&Type=" + Type;
                    break;
                case 4:
                    link = "/Pages/ListPriceItem.aspx?xml=PriceItemMenu&Type=" + Type;
                    break;
                case 7:
                    link = "/Pages/ListPriceItem.aspx?xml=PriceItemInsurance&Type=" + Type;
                    break;
                case 8:
                    link = "/Pages/ListPriceItem.aspx?xml=PriceItemService&Type=" + Type;
                    break;
            }
            Response.Redirect(link);
        }
        private void LoadPriceByType()
        {
            ddlPrice.DataSource = new PriceController().ListPriceByType(Type).Where(p=>p.Status==(int)Globals.DocumentStatus.Use).OrderBy(p=>p.PriceName);

            ddlPrice.DataTextField = "PriceName";
            ddlPrice.DataValueField = "PriceId";
            ddlPrice.DataBind();

            if (Type == 1 || Type == 2 || Type == 3)
            {
                ddlPrice.Items.Insert(0, new ListItem("--- Chọn loại vé ---", "-1"));
            }
            else if (Type == 4)
            {
                ddlPrice.Items.Insert(0, new ListItem("--- Chọn thực đơn ---", "-1"));
            }
            else if (Type == 7)
            {
                ddlPrice.Items.Insert(0, new ListItem("--- Chọn loại bảo hiểm ---", "-1"));
            }
            else if (Type == 8)
            {
                ddlPrice.Items.Insert(0, new ListItem("--- Chọn loại dịch vụ ---", "-1"));
            }
        }
       

        private void LoadSchedule()
        {
            ddlSchedule.DataSource = new ScheduleController().ListSchedule().Where(p=>p.Status==(int)Globals.DocumentStatus.Use).OrderBy(p=>p.ScheduleName);
            ddlSchedule.DataTextField = "ScheduleName";
            ddlSchedule.DataValueField = "ScheduleId";
            ddlSchedule.DataBind();

            ddlSchedule.Items.Insert(0, new ListItem("--- Chọn lịch trình ---", "-1"));
        }

        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }

        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }

        private void LoadPriceBySchedule()
        {
            ddlPrice.DataSource = new PriceController().ListPriceBySchedule(Type, _priceItemInfo.ScheduleId).Where(p=>p.Status==(int)Globals.DocumentStatus.Use).OrderBy(p=>p.PriceName);
            ddlPrice.DataTextField = "PriceName";
            ddlPrice.DataValueField = "PriceId";
            ddlPrice.DataBind();

            ddlPrice.Items.Insert(0, new ListItem("--- Chọn loại vé ---", "-1"));
        }

    }
}